package ltd.hyangyang.vegetable.service.impl;

import ltd.hyangyang.vegetable.entity.ClassificationEntity;
import ltd.hyangyang.vegetable.service.ClassificationService;
import ltd.hyangyang.vegetable.service.TrainService;
import ltd.hyangyang.vegetable.utils.FolderUploadParams;
import ltd.hyangyang.vegetable.utils.MultipartFileToFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class TrainServiceImpl implements TrainService {

    @Autowired
    private ClassificationService classificationService;

    @Override
    public void uploadFolder(String dirPath, FolderUploadParams folderUploadParams) {
        String[] strings = folderUploadParams.getRelativePath().split("/");
        for (int i=0 ; i<strings.length-1;i++){
            dirPath=dirPath+ File.separator+strings[i];
        }
        System.out.println("dirPath的值为："+dirPath);
        File file = new File(dirPath);
        if (!file.exists()){
            //创建目录
            boolean flag = file.mkdirs();
        }
        if (folderUploadParams.getTotalChunks()==1){
            //如果分片的次数为1，则保存图片
            MultipartFileToFile.saveMultipartFile(folderUploadParams.getFile(),dirPath,folderUploadParams.getFilename());
        }
    }

    @Override
    public void add(ClassificationEntity classificationEntity) {
        //查询出当前的最后一个分类
        List<ClassificationEntity> classificationEntities = classificationService.getBaseMapper().selectList(null);
        ClassificationEntity classification = classificationEntities.get(classificationEntities.size() - 1);
        classificationEntity.setClassId(classification.getClassId()+1);
        //保存新的分类
        classificationService.save(classificationEntity);
    }

}
